package com.xiangyang.entity;

import com.alibaba.fastjson2.annotation.JSONField;
import com.xiangyang.entity.vo.UserDetailInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails {
    public LoginUser (UserDetailInfo userDetailInfo) {
        this.userDetailInfo = userDetailInfo;
    }

    private UserDetailInfo userDetailInfo;
    @JSONField(read = false)
    List<SimpleGrantedAuthority> authorities;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null) {
            return authorities;
        }
        authorities = new ArrayList<>();
        for (String item : userDetailInfo.getList()) {
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(item);
            authorities.add(authority);
        }
      return authorities;
    }

    @Override
    public String getPassword() {
        return userDetailInfo.getUserPassword();
    }

    @Override
    public String getUsername() {
        return userDetailInfo.getUserPhone();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        if (userDetailInfo.getUserState()==0)
            return true;
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
